package cn.zjj.controller;

import cn.zjj.domain.MovieTicket;
import cn.zjj.domain.User;
import cn.zjj.downgrade.DowngradeUtils;
import cn.zjj.dto.UserTicketDto;
import cn.zjj.result.JsonResult;
import cn.zjj.sevice.IUserService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    //查询所有电影票
    @SentinelResource(value = "user-selectAllTicket",blockHandler = "selectAllTicketblockMethod",blockHandlerClass = DowngradeUtils.class)
    @GetMapping("/selectAllTicket")
    public JsonResult selectAllTicket(){
        List<MovieTicket> list=userService.selectAllTicket();
        return JsonResult.success(list);
    }
    //观影
    @SentinelResource(value = "user-seeMovie",blockHandler = "seeMovieblockMethod",blockHandlerClass = DowngradeUtils.class)
    @PostMapping("/seeMovie")
    public JsonResult seeMovie(@RequestBody UserTicketDto userTicketDto){
        MovieTicket movieTicket=userService.seeMovie(userTicketDto);
        return JsonResult.success(movieTicket);
    }
    //购票
    @PostMapping("/buyTicket")
    @SentinelResource(value = "user-buyTicket",blockHandler = "buyTicketblockMethod",blockHandlerClass = DowngradeUtils.class)
    public JsonResult buyTicket(@RequestBody UserTicketDto buyTicketDto){
        userService.buyTicket(buyTicketDto);
        return JsonResult.success("购票成功");
    }
    //根据电影票id查票
    @GetMapping("/selectByTicketId/{id}")
    @SentinelResource(value = "user-selectByTicketId",blockHandler = "selectByTicketIdblockMethod",blockHandlerClass = DowngradeUtils.class)
    public JsonResult selectByTicketId(@PathVariable("id") Long id){
        MovieTicket movieTicket=userService.selectByTicketId(id);
        return JsonResult.success(movieTicket);
    }
    //新增用户,并随机赠票
    @PostMapping("/save")
    @SentinelResource(value = "user-save",blockHandler = "saveblockMethod",blockHandlerClass = DowngradeUtils.class)
    public JsonResult save(@RequestBody User user){
        userService.save(user);
        return JsonResult.success("恭喜获得赠票");
    }
}
